package com.hairline.hairlinemarket.service.impl;

import com.hairline.hairlinemarket.bean.MarketCategory;
import com.hairline.hairlinemarket.bean.MarketCategoryExample;
import com.hairline.hairlinemarket.mapper.MarketCategoryMapper;
import com.hairline.hairlinemarket.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Author:杨小凯
 * Date:2023-04-20 18:46
 * Description:<描述>
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private MarketCategoryMapper categoryMapper;
    @Override
    public List queryChannel() {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.or().andPidEqualTo(0);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);
        return marketCategories;
    }

    @Override
    public List<MarketCategory> queryL1WithoutRecommend(int statr, Integer catlogListLimit) {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();

        /*List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);
        ArrayList<MarketCategory> categories = new ArrayList<>();
        for (Integer i = 0; i < catlogListLimit; i++) {
            categories.add(marketCategories.get(i));

        }*/
        criteria.andPidEqualTo(0);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);
        ArrayList<MarketCategory> categories = new ArrayList<>();
        for (Integer i = 0; i < catlogListLimit; i++) {
            categories.add(marketCategories.get(i));

        }
        return categories;
    }


    @Override
    public List<MarketCategory> queryByPid(Integer id) {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(id);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);
        return marketCategories;
    }

    @Override
    public List<MarketCategory> selectBrother(Integer id) {

        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.or().andPidEqualTo(id);
        List<MarketCategory> marketCategories = categoryMapper.selectByExample(marketCategoryExample);
        if (marketCategories==null||marketCategories.size()==0) {
            MarketCategory current = selectCurrent(id);
            Integer pid = current.getPid();
            MarketCategoryExample example = new MarketCategoryExample();
            example.or().andPidEqualTo(pid);
            marketCategories = categoryMapper.selectByExample(example);
        }
        return marketCategories;
    }

    @Override
    public MarketCategory selectCurrent(Integer id) {
        MarketCategory marketCategory = categoryMapper.selectByPrimaryKey(id);
        return marketCategory;
    }

    @Override
    public MarketCategory selectParent(Integer id) {
        MarketCategory marketCategory = selectCurrent(id);
        /*Integer pid = marketCategory.getPid();
        MarketCategory parentCategory = categoryMapper.selectByPrimaryKey(pid);
        */

        return marketCategory;
    }

    @Override
    public Integer selectGoodsCount() {

        Integer count = (int) categoryMapper.countByExample(new MarketCategoryExample());
        return count;
    }
}
